import click

from extensions import db


def register_commands(app):
    """ 注册自定义命令 """

    @app.cli.command()
    @click.option('--drop', is_flag=True, help='Create after drop.')
    def initdb(drop):
        """Initialize the database."""
        from task_app.models import JobPlan, JobEvent, User
        if drop:
            click.confirm('This operation will delete the database, do you want to continue?', abort=True)
            db.drop_all()
            click.echo('Drop tables.')

        click.echo('Initializing the database...')
        db.create_all()
        click.echo('Initialized database.')

        click.echo('Initializing the Manager...')
        fake_user()
        click.echo('Initialized Manager.')


def fake_user():
    from task_app.models import User
    user = User()
    user.username = "test"
    user.set_password("test123456")
    db.session.add(user)
    db.session.commit()
